package isnork.g2;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.naming.directory.DirContext;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

import isnork.g2.utility.Utility;
import isnork.sim.GameObject;
import isnork.sim.Observation;
import isnork.sim.SeaLifePrototype;
import isnork.sim.GameObject.Direction;

public class Satisfied implements Strategy {

	public static int TOTAL_ROUNDS = 480;
	private static double utility;
	private static final Point2D BOAT = new Point2D.Double(0.0, 0.0);
	private Direction direction_to_boat;
	double safeDegree;
	double dangerPerCell;

	Satisfied(G2Explorer diver) {
		this.safeDegree = diver.safeDegree;
		// this.safeDegree = ExplorerConfig.safeDegree;
		// used when training, safeDegree should depend on board specification
		this.dangerPerCell = diver.dangerPerCell;
	}

	@Override
	public Map<Direction, Double> process(G2Explorer diver) {

		Map<Direction, Double> utility_accumulatorExp = new HashMap<Direction, Double>();
		for (Direction d : Direction.values()) {
			utility_accumulatorExp.put(d, 0.0);
		}
		direction_to_boat = Utility.RelativeDirection(diver.location, BOAT);
		utility = diver.dangerPerCell * 9 * (1 - safeDegree)
				* ExplorerConfig.SATISFIED;

		// diver.dangerPerCell*9 is the expected danger per round if a diver
		// moves randomly
		// return to boat when satisfied, so this utility don't depend on how
		// much time left

		if ((diver.getHappiness() - diver.suffered) < diver.expHapp	* safeDegree) {
			return null;
			// when not satisfied, return null
		}

		if (diver.location.equals(BOAT))
		{
			utility_accumulatorExp.put(Direction.STAYPUT, Double.MAX_VALUE/2);
			// if you are on boat, don't go off
			//System.out.println("on board!");
		}
		else 
		{
			for (Direction d : Direction.values()) 
			{
				if (d == direction_to_boat) 
				{
					utility_accumulatorExp.put(d, utility);
				} 
				else if (Math.abs(d.getDegrees() - direction_to_boat.getDegrees()) == 45)
				{
					utility_accumulatorExp.put(d, utility * ExplorerConfig.secondChoice);
				}
				else if (Math.abs(d.getDegrees() - direction_to_boat.getDegrees()) == 90)
				{
					utility_accumulatorExp.put(d, utility * ExplorerConfig.thirdChoice);
				}
			}
		}

		return utility_accumulatorExp;
	}
}
